<?php
 
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 karPenilaian_Student
	 karPenilaian_Student_add
	 karPenilaian_Student_edit
	 */
	class Report_presensiController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Akademik";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("akademik")){
				LoginUser::redirectToLoginPage("akademik");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		}
	 
		public function actionByMonth(){
			LoginUser::getPermission("akademik_presensi_student");
			$this->subMenu = "Presensi";
			$this->layout ="//layouts/ly_blank";
			$karyawan = $this->loginUser ->karyawan;
			$department_id = $karyawan->department_id;	
			$department_name = $karyawan->department->name;					
			$karyawan_name = $karyawan->name;	
			$karyawan_id=$karyawan->id;				
						
 			$module= $this->loginUser ->module;
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE    group by department_id order by id");

			if ($departments == null){
				$department_id == "";
			}else{	
				$department_id = Yii::app()->request->getParam("department_id", $departments[0]->department_id);
			}
			
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$semester_id = Yii::app()->request->getParam("semester_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
			$absent_date = Yii::app()->request->getParam("absent_date","");		
			$absent_date_end = Yii::app()->request->getParam("absent_date_end","");							

			$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
			}			
		 
            $tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
			if ($tingkatans == null){
				$tingkatan_id == "";
			}else{
				$tingkatan_id = $tingkatan_id == "" ? $tingkatans[0]->tingkatan_id : $tingkatan_id;
			}
			
			$mcon= Yii::app()->db;						
			$kelass = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$tingkatan_id,":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}

			$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
			if ($presensi == null){
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date",date("d-M-Y"))),"d-M-Y");	
				$presensi_id=0;
			}else{
				$absent_date = $absent_date == "" ? date('d-M-Y',strtotime($presensi[0]["absent_date"])) : $absent_date;
				$presensi_id = $presensi[0]["id"];
			}
			$absent_date_end = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date_end",date("d-M-Y"))),"d-M-Y");	
//			$absent_date = date('d-M-Y',strtotime($absent_date)); 
//			$absent_date_end = date('d-M-Y',strtotime($absent_date_end)); 			
			
	 
				$mcon= Yii::app()->db;
//			cekdata
 


 
/*			//bentuk kalender	
				$todaydateset ='';	
			for ($x=1; $x<=$DayinMonth; $x++) {
				$todaystartdate = mktime(0, 0, 0, $monthstart, $x,   $yearstart);	
				$xdate = date( "w", $todaystartdate);					
				$todaydateset ='';		
				foreach ($mReportMonthCommands as $mReportMonthCommand){
					$todaydate  = $mReportMonthCommand["days"];	
					if ($x == $todaydate)
						{
						$todaydateset .=$mReportMonthCommand["sid"] . ' '.$mReportMonthCommand["name"] . ' ' .$mReportMonthCommand["presensi_status"]."<br>";	
						}		
	 			}	 

				switch ($xdate) {
					case 0:
						$viewdata = $viewdata . "<td  height=100>".$x."<br>".$todaydateset."</td>";
						break;
					case 1:
						$viewdata = $viewdata . "<td  height=100>".$x."<br>".$todaydateset."</td>";
						break;
					case 2:
						$viewdata = $viewdata . "<td  height=100>".$x."<br>".$todaydateset."</td>";
						break;
					case 3:
						$viewdata = $viewdata . "<td  height=100>".$x."<br>".$todaydateset."</td>";
						break;
					case 4:
						$viewdata = $viewdata . "<td   height=100>".$x."<br>".$todaydateset."</td>";
						break;
					case 5:
						$viewdata = $viewdata . "<td  height=100>".$x."<br>".$todaydateset."</td>";
						break;
					case 6:
						$viewdata = $viewdata . "<td height=100>".$x."<br>".$todaydateset."</td></tr><tr>";
						break;												
				}				
			}				
			$xdate = date( "w", $DayinMonth);												
			for ($x=1; $x<=$xdate-1; $x++) {
			$viewdata = $viewdata . "<td> </td>";
			}				 
				$viewdata = $viewdata . "</tr>";	
  */
			$data=array(
					"department_id"=>$department_id,
					"departments" => $departments,						
					"karyawan_name"=>$karyawan_name,
					"karyawan_id"=>$karyawan_id,										
					"tahun_ajaran_id"=>$tahun_ajaran_id,
					"tingkatans"=>$tingkatans,							
					"tingkatan_id"=>$tingkatan_id,					
					"tahunAjarans"=>$tahunAjarans,					
					"kelass"=>$kelass,
					"absent_date"=>$absent_date,		
					"absent_date_end"=>$absent_date_end,										
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"department_name"=>$department_name,
					"presensi_id"=>$presensi_id 
					);				
			$this->render('rptByMonth',$data);	
 		}
		
		public function actionByMonthList(){		
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$absent_date = Yii::app()->request->getParam("absent_date");
			$absent_date_end = Yii::app()->request->getParam("absent_date_end");			
			$absent_date = date_format(date_create_from_format( "d-M-Y", $absent_date),"Y-m-d");	
			$absent_date_end = date_format(date_create_from_format( "d-M-Y", $absent_date_end),"Y-m-d");				
			$mcon= Yii::app()->db;
			$sqlall='SELECT B.id as student_id, B.sid, P.id as presensi_id,  PS.id as id,P.karyawan_id,P.kelas_tahun_ajaran_id,PS.presensi_status,PS.notes,PS.student_id,PS.absent_date,day(PS.absent_date) as days,B.name FROM  `m_presensi` P left join m_presensi_student PS on P.id=PS.presensi_id left join v_student_kelas_tahun_ajaran  VSKTA on VSKTA .student_id = PS.student_id and P.kelas_tahun_ajaran_id=VSKTA.kelas_tahun_ajaran_id 
left join m_student B on PS.student_id=B.id where  PS.id is not null  ';
			$sqltask_count = $sqlall . " and P.kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and ( PS.absent_date between :absent_date and :absent_date_end) order by PS.absent_date";
 
			$mReportMonthCommands=$mcon->createCommand($sqltask_count);		
			$mReportMonthCommands->bindValue('absent_date',$absent_date);		
			$mReportMonthCommands->bindValue('absent_date_end',$absent_date_end);
			$mReportMonthCommands->bindValue('kelas_tahun_ajaran_id',$kelas_tahun_ajaran_id); 							
			$mReportMonthCommands=$mReportMonthCommands->queryAll();		
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mReportMonthCommands;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
				
		public function actionByStudent(){
			LoginUser::getPermission("akademik_presensi_student");
			$this->subMenu = "Presensi";
			$this->layout ="//layouts/ly_blank";
			$karyawan = $this->loginUser ->karyawan;
			$department_id = $karyawan->department_id;	
			$department_name = $karyawan->department->name;					
			$karyawan_name = $karyawan->name;	
			$karyawan_id=$karyawan->id;				
						
 			$module= $this->loginUser ->module;
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE    group by department_id order by id");

			if ($departments == null){
				$department_id == "";
			}else{	
				$department_id = Yii::app()->request->getParam("department_id", $departments[0]->department_id);
			}
			
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$student_id = Yii::app()->request->getParam("student_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
			$absent_date = Yii::app()->request->getParam("absent_date","");				
			$absent_date_end = Yii::app()->request->getParam("absent_date_end","");							

			$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
			}			
		 
            $tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
			if ($tingkatans == null){
				$tingkatan_id == "";
			}else{
				$tingkatan_id = $tingkatan_id == "" ? $tingkatans[0]->tingkatan_id : $tingkatan_id;
			}
			
			$mcon= Yii::app()->db;						
			$kelass = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$tingkatan_id,":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}
			
			$students = VStudentKelasTahunAjaran::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id  and is_active = TRUE ",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id));
 
			if ($students == null){
				$student_id == "";
			}else{
				$student_id = $student_id == "" ? $students[0]["student_id"] : $student_id;
			}	
 
			$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
			if ($presensi == null){
				$absent_date = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date",date("d-M-Y"))),"d-M-Y");	
				$presensi_id=0;
			}else{
				$absent_date = $absent_date == "" ? date('d-M-Y',strtotime($presensi[0]["absent_date"])) : $absent_date;
				$presensi_id = $presensi[0]["id"];
			}
			$absent_date_end = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("absent_date_end",date("d-M-Y"))),"d-M-Y");				
		 
			$data=array(
					"department_id"=>$department_id,
					"departments" => $departments,						
					"karyawan_name"=>$karyawan_name,
					"karyawan_id"=>$karyawan_id,										
					"tahun_ajaran_id"=>$tahun_ajaran_id,
					"tingkatans"=>$tingkatans,							
					"tingkatan_id"=>$tingkatan_id,					
					"tahunAjarans"=>$tahunAjarans,					
					"kelass"=>$kelass,
					"absent_date"=>$absent_date,					
					"absent_date_end"=>$absent_date_end,										
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"department_name"=>$department_name,
					"presensi_id"=>$presensi_id,
					"student_id"=>$student_id,
					"students"=>$students
					);				
			$this->render('rptByStudent',$data);	
 		}
		
		public function actionByStudentList(){		
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$student_id = Yii::app()->request->getParam("student_id","");			
			$absent_date = Yii::app()->request->getParam("absent_date");
			$absent_date_end = Yii::app()->request->getParam("absent_date_end");			
			$absent_date = date_format(date_create_from_format( "d-M-Y", $absent_date),"Y-m-d");	
			$absent_date_end = date_format(date_create_from_format( "d-M-Y", $absent_date_end),"Y-m-d");		
 		

			$mcon= Yii::app()->db;
			$sqlall='SELECT B.id as student_id, B.sid, P.id as presensi_id,  PS.id as id,P.karyawan_id,P.kelas_tahun_ajaran_id,PS.presensi_status,PS.notes,PS.student_id,PS.absent_date,MONTHNAME((PS.absent_date)) as months,B.name FROM  `m_presensi` P left join m_presensi_student PS on P.id=PS.presensi_id left join v_student_kelas_tahun_ajaran  VSKTA on VSKTA .student_id = PS.student_id and P.kelas_tahun_ajaran_id=VSKTA.kelas_tahun_ajaran_id 
left join m_student B on PS.student_id=B.id where  PS.id is not null  ';
			$sqltask_count = $sqlall . " and P.kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and B.id =:student_id and  ( PS.absent_date between :absent_date and :absent_date_end) order by PS.absent_date";
			$mReportMonthCommands=$mcon->createCommand($sqltask_count);		
			$mReportMonthCommands->bindValue('absent_date',$absent_date);		
			$mReportMonthCommands->bindValue('absent_date_end',$absent_date_end);
			$mReportMonthCommands->bindValue('kelas_tahun_ajaran_id',$kelas_tahun_ajaran_id); 		
			$mReportMonthCommands->bindValue('student_id',$student_id); 										
			$mReportMonthCommands=$mReportMonthCommands->queryAll();		
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mReportMonthCommands;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}		 
				
	protected function beforeRender($view){
		if ($this->subMenu != ""){
			$this->title = $this->mainMenu . " - " .$this->subMenu;
		}
		return true;
	}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>